package pages.template;

import javax.faces.application.ViewHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import javax.servlet.http.HttpServletRequest;

public class templateBean {
    private String currentUrl;
    
    public templateBean() {
    }

    public void setCurrentUrl(String currentUrl) {
        this.currentUrl = currentUrl;
    }

    public String getCurrentUrl() {
        FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext eContext = context.getExternalContext();
            ViewHandler handler = context.getApplication().getViewHandler();
            
            String prefix;
            Object requestObject = eContext.getRequest();
            if (requestObject instanceof HttpServletRequest)
            {
              prefix = getHttpUrlPrefix((HttpServletRequest)requestObject);
            }
            else
            {
              // Portlet?
              prefix = "";
            }
            
            String actionUrl = handler.getActionURL(context, context.getViewRoot().getViewId());
            
            currentUrl= prefix + actionUrl;
        return currentUrl;
    }
    
    private String getHttpUrlPrefix(HttpServletRequest request)
      {
        StringBuilder builder = new StringBuilder(32);
        builder.append(request.getScheme());
        builder.append("://");
        builder.append(request.getServerName());
        builder.append(':');
        builder.append(request.getServerPort());
        
        return builder.toString();
      }
}
